import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store';

// 消除重复进入相同路由的警报
// push方式
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// replace方式
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
};

Vue.use(VueRouter);

const routes = [
  // 登录
  {
    path: '/',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    meta: {
      hideNavBar: true,
      requireAuth: true
    }
  },
  // 项目中心
  {
    path: '/projectCenter',
    name: 'projectCenter',
    component: () => import('../views/projectCenter/index.vue'),
    meta: {
      hideNavBar: true, //是否隐藏顶部导航栏
      showTabbar: true //是否显示底部导航栏
    }
  },
  // 项目中心-项目管理
  {
    path: '/projectCenter/projectManage',
    name: 'projectManage',
    component: () => import('../views/projectCenter/projectManage/index.vue'),
    meta: {
      title: '项目管理'
    }
  },
  // 项目中心-项目管理-项目详情
  {
    path: '/projectCenter/projectManage/projectDetail',
    name: 'projectDetail',
    component: () => import('../views/projectCenter/projectManage/ProjectDetail.vue'),
    meta: {
      title: '项目详情'
    }
  },
  {
    path: '/projectCenter/projectManage/projectDetail',
    name: 'iframe',
    component: () => import('../views/projectCenter/projectManage/IFrame.vue'),
    meta: {
      title: '文件详情'
    }
  },
  // 维保中心
  {
    path: '/maintenanceCenter',
    name: 'maintenanceCenter',
    component: () => import('../views/maintenanceCenter/index.vue'),
    meta: {
      hideNavBar: true,
      showTabbar: true
    }
  },
  // 维保中心-创建工单
  {
    path: '/maintenanceCenter/addWorkOrderManage',
    name: 'addWorkOrderManage',
    component: () => import('../views/maintenanceCenter/AddWorkOrderManage.vue'),
    meta: {
      title: '创建工单'
    }
  },
  // 维保中心-工程师全部工单
  {
    path: '/maintenanceCenter/allWorkOrder',
    name: 'allWorkOrder',
    component: () => import('../views/maintenanceCenter/engineer/index.vue'),
    meta: {
      title: '全部工单'
    }
  },
  // 维保中心-工程师待接单工单
  {
    path: '/maintenanceCenter/stayWorkOrder',
    name: 'stayWorkOrder',
    component: () => import('../views/maintenanceCenter/engineer/index.vue'),
    meta: {
      title: '待接单工单'
    }
  },
  // 维保中心-工程师待处理工单
  {
    path: '/maintenanceCenter/disposeWorkOrder',
    name: 'disposeWorkOrder',
    component: () => import('../views/maintenanceCenter/engineer/index.vue'),
    meta: {
      title: '待处理工单'
    }
  },
  // 维保中心-工程师已完成工单
  {
    path: '/maintenanceCenter/completeWorkOrder',
    name: 'completeWorkOrder',
    component: () => import('../views/maintenanceCenter/engineer/index.vue'),
    meta: {
      title: '已完成工单'
    }
  },
  // 维保中心-工程师工单-工单详情
  {
    path: '/maintenanceCenter/stayWorkOrder/stayWorkDetail',
    name: 'stayWorkDetail',
    component: () => import('../views/maintenanceCenter/engineer/StayWorkDetail.vue'),
    meta: {
      title: '工单详情'
    }
  },
  // 维保中心-工单管理
  {
    path: '/maintenanceCenter/workOrderManage',
    name: 'workOrderManage',
    component: () => import('../views/maintenanceCenter/workOrderManage/index.vue'),
    meta: {
      title: '工单管理'
    }
  },
  // 维保中心-工单管理-工单详情
  {
    path: '/maintenanceCenter/workOrderManageDetail',
    name: 'workOrderManageDetail',
    component: () => import('../views/maintenanceCenter/workOrderManage/WorkOrderManageDetail.vue'),
    meta: {
      title: '工单详情'
    }
  },
  // 维保中心-工单管理-修改工单
  {
    path: '/maintenanceCenter/editWorkOrderManage',
    name: 'editWorkOrderManage',
    component: () => import('../views/maintenanceCenter/workOrderManage/EditWorkOrderManage.vue'),
    meta: {
      title: '修改工单'
    }
  },
  //维保中心-报警通知-报警通知
  {
    path: '/maintenanceCenter/alarmNotification',
    name: 'alarmNotification',
    component: () => import('../views/maintenanceCenter/alarmNotification/index.vue'),
    meta: {
      title: '报警通知'
    }
  },
  //维保中心-报警通知-报警详情
  {
    path: '/maintenanceCenter/alarmNotificationDetail',
    name: 'alarmNotificationDetail',
    component: () => import('../views/maintenanceCenter/alarmNotification/AlarmNotificationDetail.vue'),
    meta: {
      title: '报警详情'
    }
  },
  //维保中心-报警通知-报警工单详情
  {
    path: '/maintenanceCenter/AlarmWorkDetail',
    name: 'alarmWorkDetail',
    component: () => import('../views/maintenanceCenter/alarmNotification/AlarmWorkDetail.vue'),
    meta: {
      title: '报警详情'
    }
  },
  //维保中心-报警通知-报警转工单
  {
    path: '/maintenanceCenter/transforWorkOrder',
    name: 'transforWorkOrder',
    component: () => import('../views/maintenanceCenter/alarmNotification/TransforWorkOrder.vue'),
    meta: {
      title: '报警转工单'
    }
  },
  //维保中心-保养通知-保养通知
  {
    path: '/maintenanceCenter/maintenanceNotice',
    name: 'maintenanceNotice',
    component: () => import('../views/maintenanceCenter/maintenanceNotice/index.vue'),
    meta: {
      title: '保养通知'
    }
  },
  //维保中心-保养通知-保养详情
  {
    path: '/maintenanceCenter/maintenanceNoticeDetail',
    name: 'maintenanceNoticeDetail',
    component: () => import('../views/maintenanceCenter/maintenanceNotice/MaintenanceNoticeDetail.vue'),
    meta: {
      title: '保养详情'
    }
  },
  //维保中心-保养通知-保养工单详情
  {
    path: '/maintenanceCenter/MaintNoticeWorkDetail',
    name: 'maintNoticeWorkDetail',
    component: () => import('../views/maintenanceCenter/maintenanceNotice/MaintNoticeWorkDetail.vue'),
    meta: {
      title: '保养详情'
    }
  },
  //维保中心-保养通知-保养转工单
  {
    path: '/maintenanceCenter/noticeTransforWorkOrder',
    name: 'noticeTransforWorkOrder',
    component: () => import('../views/maintenanceCenter/maintenanceNotice/TransforWorkOrder.vue'),
    meta: {
      title: '保养转工单'
    }
  },
  // 维保中心-工单管理-指派工单
  {
    path: '/maintenanceCenter/assignWorkOrderManage',
    name: 'assignWorkOrderManage',
    component: () => import('../views/maintenanceCenter/workOrderManage/AssignWorkOrderManage.vue'),
    meta: {
      title: '指派工单'
    }
  },
  // 设备中心
  {
    path: '/deviceCenter',
    name: 'deviceCenter',
    component: () => import('../views/deviceCenter/index.vue'),
    meta: {
      hideNavBar: true,
      showTabbar: true
    }
  },
  // 设备中心-产线管理
  {
    path: '/deviceCenter/productLineManage',
    name: 'productLineManage',
    component: () => import('../views/deviceCenter/productLineManage/index.vue'),
    meta: {
      title: '产线管理'
    }
  },
  // 设备中心-产线管理-产线详情
  {
    path: '/deviceCenter/productLineManage/productLineDetail',
    name: 'productLineDetail',
    component: () => import('../views/deviceCenter/productLineManage/ProductLineDetail.vue'),
    meta: {
      title: '产线详情'
    }
  },
  // 设备中心-设备管理
  {
    path: '/deviceCenter/deviceManage',
    name: 'deviceManage',
    component: () => import('../views/deviceCenter/deviceManage/index.vue'),
    meta: {
      title: '设备管理'
    }
  },
  // 设备中心-设备管理-设备详情
  {
    path: '/deviceCenter/deviceManage/deviceDetail',
    name: 'deviceDetail',
    component: () => import('../views/deviceCenter/deviceManage/DeviceDetail.vue'),
    meta: {
      title: '设备详情'
    }
  },
  // 设备中心-设备管理-设备组态
  {
    path: '/deviceCenter/deviceManage/deviceScada',
    name: 'deviceScada',
    component: () => import('../views/deviceCenter/deviceManage/DeviceScada.vue'),
    meta: {
      title: '设备组态'
    }
  },
  // 设备中心-设备管理-设备详情-知识详情
  {
    path: '/deviceCenter/deviceManage/deviceDetail/repositoryDetail',
    name: 'repositoryDetail',
    component: () => import('../views/deviceCenter/deviceManage/RepositoryDetail.vue'),
    meta: {
      title: '知识详情'
    }
  },
  // 设备中心-历史数据
  {
    path: '/deviceCenter/historyData',
    name: 'historyData',
    component: () => import('../views/deviceCenter/historyData/index.vue'),
    meta: {
      title: '历史数据'
    }
  },
  // 设备中心-数据分析
  {
    path: '/deviceCenter/dataAnalyse',
    name: 'dataAnalyse',
    component: () => import('../views/deviceCenter/dataAnalyse/index.vue'),
    meta: {
      title: '数据分析'
    }
  },
  // 我的
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/my/index.vue'),
    meta: {
      hideNavBar: true,
      showTabbar: true
    }
  },
  // 我的-消息中心
  {
    path: '/my/messageCenter',
    name: 'messageCenter',
    component: () => import('../views/my/messageCenter/index.vue'),
    meta: {
      title: '消息中心'
    }
  },
  // 我的-基本信息
  {
    path: '/my/basicInfo',
    name: 'myBasicInfo',
    component: () => import('../views/my/basicInfo/index.vue'),
    meta: {
      title: '基本信息'
    }
  },
  // 我的-基本信息-修改密码
  {
    path: '/my/basicInfo/editPassword',
    name: 'editPassword',
    component: () => import('../views/my/basicInfo/EditPassword.vue'),
    meta: {
      title: '修改密码'
    }
  },
  // 我的-基本信息-修改手机号
  {
    path: '/my/basicInfo/editPhone',
    name: 'editPhone',
    component: () => import('../views/my/basicInfo/EditPhone.vue'),
    meta: {
      title: '修改手机号'
    }
  },
  // 我的-基本信息-修改手机号-下一步
  {
    path: '/my/basicInfo/editPhoneNext',
    name: 'editPhoneNext',
    component: () => import('../views/my/basicInfo/EditPhoneNext.vue'),
    meta: {
      title: '修改手机号'
    }
  },
  // 我的-基本信息-电子邮箱
  {
    path: '/my/basicInfo/editEmail',
    name: 'editEmail',
    component: () => import('../views/my/basicInfo/EditEmail.vue'),
    meta: {
      title: '电子邮箱'
    }
  },
  // 我的-反馈建议
  {
    path: '/my/feedback',
    name: 'feedback',
    component: () => import('../views/my/Feedback.vue'),
    meta: {
      title: '反馈建议'
    }
  },
  // 我的-关于我们
  {
    path: '/my/aboutUs',
    name: 'aboutUs',
    component: () => import('../views/my/AboutUs.vue'),
    meta: {
      title: '关于我们'
    }
  }
];

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
});

// 路由拦截
router.beforeEach(async (to, from, next) => {
  let token = localStorage.getItem('token');
  if (to.meta.requireAuth && !token) {
    next();
  } else {
    store.dispatch('getMessageStatusAndNum');
    if (store.state.buttons.length) {
      next();
    } else {
      await store.dispatch('generateAhthButtons');
      next();
    }
  }
});

export default router;
